from django.db import models

from apps.user.models import User

STATUS = (
    ('0', '未收藏'),
    ('1', '已收藏')
)


class Tag(models.Model):
    name = models.CharField(max_length = 30)
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column = 'user_id', verbose_name = '用户',
                                related_name = 'user_tag')

    def __str__(self):
        return f'{self.name}'

    class Meta:
        db_table = 'tag'
        verbose_name_plural = '分类表'


class Works(models.Model):
    name = models.CharField(max_length = 100, db_column = 'name', verbose_name = '名称')
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column = 'user_id', verbose_name = '用户',
                                related_name = 'user_works')
    url = models.CharField(max_length = 255, verbose_name = '链接', db_column = 'url')
    tags = models.ManyToManyField(Tag, related_name = 'works_tags', db_column = 'tags', verbose_name = '分类')

    class Meta:
        db_table = 'works'
        verbose_name_plural = '作品表'


class Collection(models.Model):
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column = 'user_id', verbose_name = '用户',
                                related_name = 'user_collection')
    work_id = models.ForeignKey(Works, models.DO_NOTHING, db_column = 'work_id', verbose_name = '作品',
                                related_name = 'work_collection')
    status = models.CharField(choices = STATUS, max_length = 1, verbose_name = '收藏状态', db_column = 'status')

    class Meta:
        db_table = 'collection'
        verbose_name_plural = '收藏表'


class Comment(models.Model):
    user_id = models.ForeignKey(User, models.DO_NOTHING, db_column = 'user_id', verbose_name = '用户',
                                related_name = 'user_comment')
    work_id = models.ForeignKey(Works, models.DO_NOTHING, db_column = 'work_id', verbose_name = '作品',
                                related_name = 'work_comment')
    message = models.TextField(db_column = 'message', verbose_name = '评论')
    time = models.DateTimeField(db_column = 'time', verbose_name = '留言时间', null = True)

    class Meta:
        db_table = 'comment'
        verbose_name_plural = '留言表'
